<?php
namespace app\web\controller\admin;

use app\admin\library\AdminController;

class Block extends AdminController{
    public function onIndex(){
        $this->pageTitle = '区块';
        $this->pageIcon = 'fa-th-large';
        return $this->display();
    }

    public function onGroup(){
        $this->pageTitle = '区块分组管理';
        return $this->display();
    }

    public function onGroupTree(){
        $part = $this->model('BlockGroup');
        
        $list = $part->getTreeData(0);
        return ['tree' => $list];
    }

    public function onGroupSave(){
        
        $id = $this->post('blockgroup_id',$this->check('ID')->type('int'));
        $data['title']=  $this->post('title',$this->check('title')->type('string')->required());
        $data['parent_id'] = $this->post('parent_id',$this->check()->type('int')->required());
        $data['brief'] = $this->post('brief',$this->check()->type('string'));

        $BlockGroup = $this->model('BlockGroup');

        if ($id){
        
            $oldData = $BlockGroup->getId($id);

            $childrenList = $BlockGroup->getKeyCollect($oldData['blockgroup_id']);
        
            if (in_array($data['parent_id'],$childrenList)){
                throw new \Exception('不能选择子栏目作为父栏目');
            }
        
            if ($BlockGroup->updateId($id,$data)){
                $this->log('编辑区块分组', ['id'=>$id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('编辑区块分组失败');
            }
        
        }else{
            
            if ($BlockGroup->insert($data)){
                $this->log('添加区块分组', ['id'=>$id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('添加区块分组失败');
            }
        }         
    }

    public function onGroupDel(){
        $id = $this->post('blockgroup_id',$this->check('ID')->type('int')->required());
        $BlockGroup = $this->model('BlockGroup');


        if ($BlockGroup->where('parent_id',$id)->count()>0){
            throw new \Exception('请先删除子分组');
        }

        $Block =  $this->model('Block');

        if ($Block->where('blockgroup_id',$id)->count()>0){
            throw new \Exception('请先删除当前分组下的所有区块');
        }

        if ($BlockGroup->deleteId($id)){
            $this->log('删除区块分组', ['id'=>$id]);
            return true;
        }else{
            throw new \Exception('删除区块分组失败');
        }
    }

    public function onList(){
        $blockgroup_id = $this->post('blockgroup_id',$this->check()->type('int'));
        $keyword =  $this->post('keyword',$this->check()->type('string'));
        $block = $this->model('Block');
        $pageCurrent = $this->post('page_current',$this->check('当前页')->default(1)->type('int'));
        

        $model = $this->model('Model');

        $block->field();
        $block->field($model->getField(['title'=>'model_title']));
        $block->join($model,$block->getField('model_id') .' = '.$model->getKey());


        if ($blockgroup_id){
            $block->where('blockgroup_id',$blockgroup_id);
        }

        if ($keyword){
            $block->where('title','like','%'.$keyword.'%');
        }

        $re['list'] = $block->page($pageCurrent,20)->order('sort','DESC')->getData();
        $re['count'] = $block->pageCount;
        $re['total'] = $block->total;

        return $re;
    }


    public function onSave(){
        $this->power('web.block');
        $id = $this->post('block_id',$this->check('ID')->type('int'));
        $data['title']=  $this->post('title',$this->check('title')->type('string')->required());
        $data['model_id']=  $this->post('model_id',$this->check('model_id')->type('string')->required());
        $data['blockgroup_id']=  $this->post('blockgroup_id',$this->check('blockgroup_id')->type('string')->required());
        $data['brief'] = $this->post('brief',$this->check()->type('string'));


        $Block = $this->model('Block');

        if ($id){
            if ($Block->updateId($id,$data)){
                $this->log('编辑区块', ['id'=>$id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('编辑区块失败');
            }
        }else{
            if ($Block->insert($data)){
                $this->log('添加区块', ['id'=>$id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('添加区块失败');
            }
        }
    }

    public function onDel(){
        $id = $this->post('block_id',$this->check('ID')->type('int')->required());
        $Block = $this->model('Block');
        if ($Block->deleteId($id)){
            $this->log('删除区块', ['id'=>$id]);
            return true;
        }else{
            throw new \Exception('删除区块失败');
        }
    }


    public function onItem(){
        
        $block_id = $this->get('block',$this->check('ID')->type('int')->required());

        $Block = $this->model('Block');
        $blockData = $Block->getId($block_id);

        if (!$blockData){
            throw new \Exception('区块不存在');
        }

        $this->set('blockData',$blockData);
        $this->set('block_id',$block_id);


        $this->pageTitle = ['区块', $blockData['title']];
        return $this->display();
    }

    public function onItemList(){
        $block_id = $this->post('block_id',$this->check('ID')->type('int')->required());
        $keyword =  $this->post('keyword',$this->check()->type('string'));
        $pageCurrent = $this->post('page_current',$this->check('当前页')->default(1)->type('int'));
        $Block = $this->model('BlockItem');

        if ($block_id){
            $Block->where('block_id',$block_id);
        }
        if ($keyword){
            $Block->where('title','like','%'.$keyword.'%');
        }

        $re['list'] = $Block->order('sort','DESC')->getData();

        return $re;
    }

    public function onItemEdit(){
        $item_id = $this->get('item',$this->check('ID')->type('int'));
        $block_id = $this->get('block',$this->check('ID')->type('int'));

        if (!$block_id && !$item_id){
            throw new \Exception('参数错误');
        }

        $pageTitle = '添加';
        $blockTitle = '';
        if ($item_id){
            $itemData = $this->model('BlockItem')->getId($item_id);
            if (!$itemData){
                throw new \Exception('区块内容不存在');
            }
            
            $pageTitle = '编辑';
            
            $this->set('blockitem_id',$item_id);
            $block_id = $itemData['block_id'];
            $this->set('item',$itemData);

            try{
                $value = json_decode($itemData['value'],true);
            }catch(\Exception $e){
                $value = [];
            }

            $this->set('value',$value);

        }

        $blockData = $this->model('Block')->getId($block_id);
        
        if (!$blockData){
            throw new \Exception('区块不存在');
        }
        $blockTitle = $blockData['title'];
        $this->set('model_id',$blockData['model_id']);
        
        $this->set('block_id',$block_id);
        $this->pageTitle = ['区块',$blockTitle,$pageTitle];
        return $this->display('admin.block_item_edit');
    }

    public function onItemSave(){
        $this->power('web.block');
        $item_id = $this->post('blockitem_id',$this->check('ID')->type('int'));
        $data['block_id']=  $this->post('block_id',$this->check('block_id')->type('int')->required());
        $data['title']=  $this->post('title',$this->check('title')->type('string')->required());
        $data['value'] = json_encode($this->post('value',[]));
        if ($item_id){
            if ($this->model('BlockItem')->updateId($item_id,$data)){
                $this->log('编辑区块内容', ['id'=>$item_id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('编辑区块内容失败');
            }
        }else{

            $data['sort'] = $this->model('BlockItem')->where('block_id',$data['block_id'])->max('sort')+1;

            if ($this->model('BlockItem')->insert($data)){
                $this->log('添加区块内容', ['id'=>$item_id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('添加区块内容失败');
            }
        }
    }

    public function onItemDel(){
        $block_id =  $this->post('block_id',$this->check()->type('int')->required());
        $blockitem_id =  $this->post('blockitem_id',$this->check()->type('int')->required());
        $blockItem = $this->model('BlockItem');

        $data = $blockItem->getId($blockitem_id);

        if (!$data){
            throw new \Exception('数据不存在');
        }

        if ($data['block_id'] != $block_id){
            throw new \Exception('数据不存在');
        }

        if ($blockItem->deleteId($blockitem_id)){

            $this->log('删除了区块内容',$data);
            return true;
        }else{
            throw new \Exception('删除失败');
        }
        
    }

    public function onItemSort(){
        $block_id =  $this->post('block_id',$this->check()->type('int')->required());
        $ids = $this->post('ids',$this->check()->type('array')->required());

        if (count($ids)<2){
            throw new \Exception('排序失败');
        }

        $blockItem = $this->model('BlockItem');

        $blockItem->where('block_id',$block_id)->switchSort($ids,'sort','desc');

        return true;

    }
}